package com.nulldev.util.java.swing.extSwing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JList;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

import com.nulldev.util.java.swing.font.FontManager;

public class FontListCellRenderer extends DefaultListCellRenderer {
	@Override
	public Component getListCellRendererComponent(
	        JList<?> list,
	        Object value,
	        int index,
	        boolean isSelected,
	        boolean cellHasFocus)
	    {
	        setComponentOrientation(list.getComponentOrientation());

	        Color bg = null;
	        Color fg = null;

	        JList.DropLocation dropLocation = list.getDropLocation();
	        if (dropLocation != null
	                && !dropLocation.isInsert()
	                && dropLocation.getIndex() == index) {

	            bg = sun.swing.DefaultLookup.getColor(this, ui, "List.dropCellBackground");
	            fg = sun.swing.DefaultLookup.getColor(this, ui, "List.dropCellForeground");

	            isSelected = true;
	        }

	        if (isSelected) {
	            setBackground(bg == null ? list.getSelectionBackground() : bg);
	            setForeground(fg == null ? list.getSelectionForeground() : fg);
	        }
	        else {
	            setBackground(list.getBackground());
	            setForeground(list.getForeground());
	        }

	        final String text = (value == null) ? "" : value.toString();
	        final Font f = (overridenSize == -1 ? FontManager.getFont(text) : FontManager.getFont(text).deriveFont((float) overridenSize));
	        if (value instanceof Icon) {
	            setIcon((Icon)value);
	            setText("");
	        }
	        else {
	            setIcon(null);
	            setText((textDemo ? "The quick brown fox jumps over the lazy dog." : text));
	        }

	        setEnabled(list.isEnabled());
	        if (f.equals(FontManager.defaultFont(12))) {
	        	setFont(list.getFont());
	        }else {
	        	setFont(f);
	        }

	        Border border = null;
	        if (cellHasFocus) {
	            if (isSelected) {
	                border = sun.swing.DefaultLookup.getBorder(this, ui, "List.focusSelectedCellHighlightBorder");
	            }
	            if (border == null) {
	                border = sun.swing.DefaultLookup.getBorder(this, ui, "List.focusCellHighlightBorder");
	            }
	        } else {
	            border = getNoFocusBorder();
	        }
	        setBorder(border);

	        return this;
	    }
	
	private final Border SAFE_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
    private final Border DEFAULT_NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
	
	private Border getNoFocusBorder() {
        Border border = sun.swing.DefaultLookup.getBorder(this, ui, "List.cellNoFocusBorder");
        if (System.getSecurityManager() != null) {
            if (border != null) return border;
            return SAFE_NO_FOCUS_BORDER;
        } else {
            if (border != null &&
                    (noFocusBorder == null ||
                    noFocusBorder == DEFAULT_NO_FOCUS_BORDER)) {
                return border;
            }
            return noFocusBorder;
        }
    }
	
	@Override
	public void paintComponent(final Graphics g) {
		((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		super.paintComponent(g);
	}
	
	private int overridenSize = -1;
	
	public void resetFontSize() {
		this.overridenSize = -1;
	}
	
	public void setFontSize(final int fontSize) {
		this.overridenSize = fontSize;
	}
	
	private boolean textDemo;
	public void setTextDemo(final boolean enabled) {
		this.textDemo = enabled;
	}
	
	public boolean isTextDemo() {
		return this.textDemo;
	}
}
